package com.sks.web.action.house;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.springframework.stereotype.Controller;

import com.sks.bean.PageView;
import com.sks.bean.QueryResult;
import com.sks.bean.pojo.Metro;
import com.sks.bean.pojo.MetroStation;
import com.sks.service.house.MetroService;
import com.sks.utils.SiteUrl;
import com.sks.utils.StringUtil;
import com.sks.web.action.BasicAction;
import com.sks.web.action.privilege.Permission;
import com.sks.web.formbean.house.MetroForm;

import flexjson.JSONSerializer;

@Controller("/cms/house/metro")
public class MetroAction extends BasicAction {
	@Resource(name = "metroServiceBean")
	private MetroService metroService;

	/**
	 * 验证public ActionForward add(ActionMapping, ActionForm, HttpServletRequest,
	 * HttpServletResponse)方法 如果存在同名的地铁线名称，则返回错误信息
	 * */
	public void beforeadd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionMessages errors = new ActionMessages();
		MetroForm formbean = (MetroForm) form;
		
		if (StringUtil.isEmpty(formbean.getName())) {
			errors.add("name", new ActionMessage("errors.required", getMessage("info.metro.field.name")));
		}
		
		List params = new ArrayList();
		params.add(formbean.getName());
		StringBuffer jpql = new StringBuffer("o.name = ?" + params.size());
		if (EDIT.equals(formbean.getMethod())) {
			params.add(formbean.getMetroId());
			jpql.append(" and o.metroId <> ?" + params.size());
		}
		QueryResult<Metro> m = metroService.getScrollData(-1, -1, jpql.toString(),
				params.toArray());
		if (null != m && m.getResultlist().size() > 0) {
			// 提示信息
			errors.add("name", new ActionMessage("errors.repeat", getMessage("info.metro.field.name")));
			request.setAttribute(ACTION, formbean.getMethod());
		}
		saveErrors(request, errors);
	}

	@Permission(module = "info", privilege = "view")
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MetroForm formbean = (MetroForm) form;
		StringBuffer jpql = new StringBuffer("");
		List<Object> params = new ArrayList<Object>();
		PageView<Metro> pageView = new PageView<Metro>(12, formbean.getPage());
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("metroId", "asc");
		QueryResult<Metro> qr = metroService.getScrollData(pageView
				.getFirstResult(), pageView.getMaxresult(), jpql.toString(),
				params.toArray(), orderby);
		pageView.setQueryResult(qr);
		request.setAttribute("pageView", pageView);
		this.setReadOnly(request, "list");
		return mapping.findForward(LIST);
	}

	@Permission(module = "info", privilege = "update")
	public ActionForward addUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.saveToken(request);
		MetroForm formbean = (MetroForm) form;
		formbean.setMethod(ADD);
		return mapping.findForward(EDIT);
	}

	@Permission(module = "info", privilege = "view")
	public ActionForward editUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.saveToken(request);
		MetroForm formbean = (MetroForm) form;
		Metro metro = metroService.find(formbean.getMetroId());
		copyProperties(metro, formbean);
		formbean.setMethod(EDIT);
		this.setReadOnly(request, "editUI");
		return mapping.findForward(EDIT);
	}

	@Permission(module = "info", privilege = "update")
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MetroForm formbean = (MetroForm) form;
		Metro metro = new Metro();
		copyProperties(formbean, metro);
		metroService.save(metro);
		request.setAttribute(MESSAGE, getMessage("display.add") + getMessage("info.metro") + getMessage("display.success"));
		request.setAttribute("urladdress", SiteUrl
				.readUrl("com.sks.web.action.house.metro"));
		return mapping.findForward(MESSAGE);
	}

	public void beforeedit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MetroForm formbean = (MetroForm) form;
		this.beforeadd(mapping, formbean, request, response);
	}

	@Permission(module = "info", privilege = "update")
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MetroForm formbean = (MetroForm) form;
		Metro metro = metroService.find(formbean.getMetroId());
		metro.setName(formbean.getName());
		metroService.update(metro);
		request.setAttribute(MESSAGE, getMessage("display.edit") + getMessage("info.metro") + getMessage("display.success"));
		request.setAttribute("urladdress", SiteUrl
				.readUrl("com.sks.web.action.house.metro"));
		return mapping.findForward(MESSAGE);
	}

	@Permission(module = "info", privilege = "update")
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MetroForm formbean = (MetroForm) form;
		metroService.delete(formbean.getMetroId());
		request.setAttribute(MESSAGE, getMessage("display.delete") + getMessage("info.metro") + getMessage("display.success"));
		request.setAttribute("urladdress", SiteUrl
				.readUrl("com.sks.web.action.house.metro"));
		return mapping.findForward(MESSAGE);
	}
	
	@Permission(module = "info", privilege = "view")
	public ActionForward getStations(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MetroForm formbean = (MetroForm) form;
		if (null == formbean.getMetroId() || 0 == formbean.getMetroId()) {
			response.getWriter().write("[{\"name\":\"\",\"areaId\":\"\"}]");
			return null;
		}
		Metro topMetro = metroService.find(formbean.getMetroId());
		List<MetroStation> mss = topMetro.getMetroStationsForMetroId();
		JSONSerializer json = new JSONSerializer();
		String jsonStr = json.exclude("*.class", "metroByMetroId", "metroByMetroId1",
				"isIntersect", "remark", "metroId", "metroId1").include("stationId", "name").serialize(mss);
		response.getWriter().write(jsonStr);
		return null;
	}
}